package com.party.admin.task;

import com.party.common.paging.Page;
import com.party.common.utils.DateUtils;
import com.party.common.utils.StringUtils;
import com.party.core.model.analysis.StatisticsEventFlow;
import com.party.core.model.analysis.StatisticsPageFlow;
import com.party.core.model.analysis.StatisticsUser;
import com.party.core.model.system.SysConfig;
import com.party.core.service.analysis.IStatisticsEventFlowService;
import com.party.core.service.analysis.IStatisticsPageFlowService;
import com.party.core.service.analysis.IStatisticsUserService;
import com.party.core.service.system.ISysConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @program: party
 * @description: 统计模块数据清理
 * @author: Hua Qunhao
 * @create: 2018-12-25 16:47
 */
@Component("quartzStatisticsDataClean")
public class QuartzStatisticsDataClean {
    @Autowired
    IStatisticsEventFlowService statisticsEventFlowService;

    @Autowired
    IStatisticsPageFlowService statisticsPageFlowService;

    @Autowired
    IStatisticsUserService statisticsUserService;

    @Autowired
    ISysConfigService sysConfigService;

    protected static Logger logger = LoggerFactory.getLogger(QuartzStatisticsDataClean.class);

    //若没有配置时间,默认是当前时间的前3个月
    private static String queryDate = DateUtils.formatDate(DateUtils.stepMonth(new Date(),-3));
    //若没有配置数量，默认是一百万
    private static Integer cleanDataNum = 1000000;

    //时间code
    private static String CLEANENDTIME = "CLEANENDTIME";
    //数量code
    private static String CLEANENDNUM = "CLEANENDNUM";

    /**
    * @Description: 统计页面详情表清理
    * @Param:  
    * @return:  
    * @Author: Hua Qunhao
    * @Date: 2018/12/25
    */ 
    public void tjPageFlowClean(){
        logger.info("统计页面详情表清理----------------------");
        long l1 = System.currentTimeMillis();

        SysConfig queryDateConfig = sysConfigService.findByCode(CLEANENDTIME);
        if(null!=queryDateConfig && StringUtils.isNotBlank(queryDateConfig.getValue())){
            //有配置时间
            queryDate = queryDateConfig.getValue();
        }

        SysConfig cleanDataNumDateConfig = sysConfigService.findByCode(CLEANENDNUM);

        if(null!=cleanDataNumDateConfig && StringUtils.isNotBlank(cleanDataNumDateConfig.getValue())){
            cleanDataNum = Integer.parseInt(cleanDataNumDateConfig.getValue());
        }

        int totalCount = statisticsPageFlowService.queryPageFlowCountByDate(queryDate,cleanDataNum);
        logger.info("总数" + totalCount);

        Page page = new Page();
        page.setPage(1);
        page.setLimit(10000);
        page.setTotalCount(totalCount);

        for (int i = 1; i <= page.getTotalPages(); i++) {
            statisticsPageFlowService.deletePageFlowByQueryDate(queryDate,10000);
        }
        logger.info("用时：" + (System.currentTimeMillis() - l1));
        logger.info("统计页面详情表清理----------------------");
    }
    /** 
    * @Description: 统计事件详情表清理
    * @Param:  
    * @return:  
    * @Author: Hua Qunhao
    * @Date: 2018/12/25
    */ 
    public void tjEventFlowClean(){
        logger.info("统计事件详情表清理----------------------");
        long l1 = System.currentTimeMillis();

        SysConfig queryDateConfig = sysConfigService.findByCode(CLEANENDTIME);
        if(null!=queryDateConfig && StringUtils.isNotBlank(queryDateConfig.getValue())){
            //有配置时间
            queryDate = queryDateConfig.getValue();
        }

        SysConfig cleanDataNumDateConfig = sysConfigService.findByCode(CLEANENDNUM);

        if(null!=cleanDataNumDateConfig && StringUtils.isNotBlank(cleanDataNumDateConfig.getValue())){
            cleanDataNum = Integer.parseInt(cleanDataNumDateConfig.getValue());
        }

        int totalCount = statisticsEventFlowService.queryEventFlowCountByDate(queryDate,cleanDataNum);
        logger.info("总数" + totalCount);

        Page page = new Page();
        page.setPage(1);
        page.setLimit(10000);
        page.setTotalCount(totalCount);

        for (int i = 1; i <= page.getTotalPages(); i++) {
            statisticsEventFlowService.deleteEventFlowByQueryDate(queryDate,10000);
        }
        logger.info("用时：" + (System.currentTimeMillis() - l1));
        logger.info("统计事件详情表清理----------------------");
    }
    /** 
    * @Description: 统计用户表清理
    * @Param:  
    * @return:  
    * @Author: Hua Qunhao
    * @Date: 2018/12/25
    */ 
    public void tjUserClean(){
        logger.info("统计用户表清理----------------------");
        long l1 = System.currentTimeMillis();


        SysConfig queryDateConfig = sysConfigService.findByCode(CLEANENDTIME);
        if(null!=queryDateConfig && StringUtils.isNotBlank(queryDateConfig.getValue())){
            //有配置时间
            queryDate = queryDateConfig.getValue();
        }

        SysConfig cleanDataNumDateConfig = sysConfigService.findByCode(CLEANENDNUM);

        if(null!=cleanDataNumDateConfig && StringUtils.isNotBlank(cleanDataNumDateConfig.getValue())){
            cleanDataNum = Integer.parseInt(cleanDataNumDateConfig.getValue());
        }

        int totalCount = statisticsUserService.queryUserCountByDate(queryDate,cleanDataNum);
        logger.info("总数" + totalCount);

        Page page = new Page();
        page.setPage(1);
        page.setLimit(10000);
        page.setTotalCount(totalCount);

        for (int i = 1; i <= page.getTotalPages(); i++) {
            statisticsUserService.deleteUserByQueryDate(queryDate,10000);
        }
        logger.info("用时：" + (System.currentTimeMillis() - l1));
        logger.info("统计用户表清理----------------------");
    }
}
